package com.hfi.controller;

import com.hfi.Response.EmployeeResponse;
import com.hfi.model.*;
import com.hfi.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ChangLiang
 * @date 2018/8/22
 */
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("addOrUpdateEmp")
    public Employee addOrUpateEmp(@RequestBody Employee employee) {
        return employeeService.addEmp(employee);
    }

    @GetMapping("/getEmployeeById")
    public Employee getEmployeeById(@RequestParam Long id) {
        return employeeService.getEmployeeById(id);
    }

    @GetMapping("/findById")
    public EmployeeNameOnly findById(@RequestParam Long id) {
        return employeeService.findById(id);
    }


    @PostMapping("/selectPageEmployee")
    public EmployeeResponse selectPageEmployee(@RequestBody Employee employee) {
        EmployeeResponse response = new EmployeeResponse();
        Page<Employee> employees = employeeService.selectPageEmployee(employee);
        response.setList(employees.getContent());
        response.setTotalResult(employees.getTotalElements());
        return response;
    }

    @PostMapping("/selectPageEmployeeNameOnly")
    public List<EmployeeNameOnlyDto> selectPageEmployeeNameOnly(@RequestBody Employee employee) {
        List<EmployeeNameOnlyDto> employeeNameOnlies = employeeService.selectPageEmployeeNameOnlyDto(employee);
        // 获取totalElement
        System.out.println((employeeService.getEmployeeCount()));
        return employeeNameOnlies;
    }

    @PostMapping("/selectPageEmployeeEntityManager")
    public EmployeeResponse selectPageEmployeeEntityManager(@RequestBody Employee employee) {
        EmployeeResponse response = new EmployeeResponse();
        List<Employee> employees = employeeService.selectPageEmployeeEntityManager(employee);
        response.setList(employees);
        response.setTotalResult(employeeService.getEmployeeCount());
        return response;
    }

    @PostMapping("/selectPageEmployeeEntityManager2")
    public List<EmployeeNameOnlyDto> selectPageEmployeeEntityManager2(@RequestBody Employee employee) {
        List<EmployeeNameOnlyDto> employeeNameOnlyDtos = employeeService.selectPageEmployeeEntityManager2(employee);
        // 获取totalElement
        System.out.println((employeeService.getEmployeeCount()));
        return employeeNameOnlyDtos;
    }

    @PostMapping("/updateEmailById")
    public Integer updateEmailById(@RequestBody Employee employee) {
        return employeeService.updateEmailById(employee);
    }

    @GetMapping("getEmployeeById2")
    public Employee getEmployeeById2(@RequestParam Long id) {
        return employeeService.getEmployeeById2(id);
    }


    @PostMapping("addOrUpdateTwoEmp")
    public Employee addOrUpateTwoEmp(@RequestBody Employee employee) {
        return employeeService.saveTwoEmployee(employee);
    }

    @GetMapping("getEmployeeMixDtoById")
    public EmployeeMixDto getEmployeeMixDtoById(@RequestParam String mixId) {
        return employeeService.getEmployeeMixDto(mixId);
    }

    @PostMapping("/getPageEmployeeMixDto")
    public List<EmployeeMixDto> getPageEmployeeMixDto(@RequestBody Employee employee) {
        List<EmployeeMixDto> list = employeeService.getPageEmployeeMixDto(employee);
        // 获取totalElement
        System.out.println((employeeService.getEmployeeCount()));
        return list;
    }

    @GetMapping("/selectEmployeeAndLog")
    public List<EmployeeAndLog> selectEmployeeAndLog(Long id) {
        List<EmployeeAndLog> employeeAndLogs = employeeService.selectEmployeeAndLog(id);
        return employeeAndLogs;
    }
}
